<template>
  <el-form :model="loginForm" :rules="formRules" ref="loginForm" label-width="100px" class="demo-ruleForm login-container">
  <h3 class="title">乐趣生活控制台</h3>
  <el-form-item label="用户名" prop="account">
      <el-input v-model="loginForm.account" v-on:keyup.enter.native="login('loginForm')"></el-input>
    </el-form-item>
    <el-form-item label="密码" prop="pass">
      <el-input type="password" v-model="loginForm.pass" auto-complete="off" @keyup.enter.native="login('loginForm')"></el-input>
    </el-form-item>
    <el-form-item>
      <el-button icon="upload" :loading="loadingState" type="success" @click="login('loginForm')" :disabled="loginDisabled">提交</el-button>
      <el-button icon="delete" @click="resetForm('loginForm')">重置</el-button>
    </el-form-item>
  </el-form>
</template>

<script>
  import ax, { errhandler } from '@/api/_axios-config'
  import * as apis from '@/api/_api'

  export default {
    data () {
      var isDisabled = () => {
        if (this.loginForm.account === '' || this.loginForm.pass === '') {
          this.loginDisabled = true
        } else {
          this.loginDisabled = false
        }
      }

      var validateAccount = (rule, value, callback) => {
        isDisabled()

        if (value === '') {
          callback(new Error('请输入用户名'))
        }

        callback()
      }

      var validatePass = (rule, value, callback) => {
        isDisabled()

        if (value === '') {
          callback(new Error('请输入密码'))
        }
        callback()
      }

      var canLogin = (rule, value, callback) => {
        isDisabled()
        callback()
      }

      return {
        loginForm: {
          account: '',
          pass: ''
        },
        formRules: {
          account: [
            { validator: validateAccount, trigger: 'blur' },
            { validator: canLogin, trigger: 'change' }
          ],
          pass: [
            { validator: validatePass, trigger: 'blur' },
            { validator: canLogin, trigger: 'change' }
          ]
        },
        loadingState: false,
        loginDisabled: true
      }
    },
    methods: {
      login (formName) {
        this.$refs[formName].validate((valid) => {
          if (valid) {
            // 加载状态
            this.loadingState = true

            // 取值
            let account = this.$refs[formName].model.account
            let pass = this.$refs[formName].model.pass

            var data = apis.parameters({
              'AdminName': account,
              'Password': pass
            })

            ax.post(apis.LOGIN, data).then(
              response => {
                this.loadingState = false
                var res = response.data
                var storage = window.localStorage
                if (res.BoolStatus) {
                  storage.setItem('token', res.Data)
                  storage.setItem('admin', account)

                  this.$router.push({ name: 'dashboard' })
                } else {
                  this.$message({
                    message: res.Message,
                    type: 'error'
                  })
                }
              }
            ).catch(
              err => {
                this.loadingState = false
                errhandler(err)
              }
            )
            // 登录
          } else {
            this.$message({
              showClose: true,
              message: '请正确填写账户信息',
              type: 'error'
            })
            return false
          }
        })
      },
      resetForm (formName) {
        this.$refs[formName].resetFields()
      }
    }
  }
</script>

<style lang="styl">
  #app{
    min-width: 0px;
  }

  .login-container {
    box-shadow: 0 0px 8px 0 rgba(0, 0, 0, 0.06), 0 1px 0px 0 rgba(0, 0, 0, 0.02);
    -webkit-border-radius: 5px;
    border-radius: 5px;
    -moz-border-radius: 5px;
    background-clip: padding-box;
    margin: 180px auto;
    width: 350px;
    padding: 35px 35px 15px 35px;
    background: #fff;
    border: 1px solid #eaeaea;
    box-shadow: 0 0 25px #cac6c6;

    //标题
    .title {
      margin: 0px auto 40px auto;
      text-align: center;
      color: #505458;
    }

    .remember {
      margin: 0px 0px 35px 0px;
    }
  }
</style>
